/**
 * 响应归一化 不仅要处理正常响应，还要处理非正常响应
 */
import SERVER from '../lib/server3'

export const mapActions = API => API.reduce((pre, serviceName) => {
  pre[serviceName] = ({ commit }, serviceArguments) => {
    return SERVER[serviceName](serviceArguments).then(res => {
      commit(serviceName, res.data)
      // 响应归一化 正常响应
      if (res.code === 0) {
        return {
          ok: true,
          payload: res,
        }
      }

      // 响应归一化 非正常响应及各种错误
      return {
        ok: false,
        payload: res,
      }
    })
  }
  return pre
}, {})

export const mapMutations = MAP => Object.keys(MAP).reduce((pre, serviceName) => {
  pre[serviceName] = (state, data) => {
    state[MAP[serviceName]] = data
  }
  return pre
}, {})